<script setup>
import {
    reactive,
    ref,
    onMounted,
    getCurrentInstance,
    onUnmounted,
    watch,
    nextTick,
} from "vue";
import { useRoute, useRouter } from "vue-router";
import { showFailToast, showToast } from "vant";
import topAvatar from "@/assets/img/model-person.png";
import userAvatar from "@/assets/img/avatar.png";
// import iconCopy from "@/assets/img/icon-copy.png";
import { useChatStore } from "@/store/index";
const store = useChatStore();
const {
    proxy: { $http },
} = getCurrentInstance();
const route = useRoute();
const router = useRouter();
const props = defineProps({
    data: {
        type: Array,
        default: null,
    },
    loading: {
        type: Object,
        default: null,
    },
});
const dataVal = reactive({
    messages: [],
    topAvatar,
    userAvatar,
    currentResponse: false,
    isAIResponding: false,
    isVoice: false,
    // iconCopy,
    scrollTop: 0
});
watch(
    () => props.data,
    (newVal) => {
        if (newVal) {
            console.info("dataVal.messages::::::::::", newVal);
            if (newVal.length === 0) {
                dataVal.messages = [];
                return;
            }else if (newVal.length > 0) {
                dataVal.messages = newVal;
                // dataVal.isAIResponding = true;
                dataVal.currentResponse = false;
                // 滚动到底部
                nextTick(() => {
                    dataVal.scrollTop = 99999;
                });
            }
        } else {
            dataVal.messages = [];
        }
    },
    { immediate: true }
);
const loading = (one, two, isVoice) => {
    console.log("loading", one, two, isVoice);
    dataVal.isAIResponding = one;
    dataVal.currentResponse = two;
};
defineExpose({
    loading,
});
const messageDiv = ref(null);
let audioInstance = ref(null);

// 清理音频资源
const cleanupAudio = () => {
    if (audioInstance.value) {
        URL.revokeObjectURL(audioInstance.value.src);
        audioInstance.value = null;
    }
};

// 组件卸载时清理
onUnmounted(() => {
    cleanupAudio();
});
onMounted(async () => {
    dataVal.messages = props.data;
});
</script>
<template>
    <scroll-view class="chat-container mb-4" scroll-y="true" :scroll-top="dataVal.scrollTop">
        <div v-for="(message, index) in dataVal.messages" :key="index" class="message d-flex align-items-start"
            :class="message.isSent ? 'sent' : 'received'">
            <div class="img" v-if="!message.isSent"></div>
            <van-icon name="smile-o" color="rgb(88, 94, 241)" size="35px" v-else class="img" />

            <div class="message-content text-l">
                <!-- 使用 v-html 显示处理后的 HTML -->
                <div ref="messageDiv" v-html="message.text"></div>
            </div>
            <div class="d-flex space-between align-items-center width100">
                <div class="message-time">{{ message.time }}</div>
            </div>
        </div>

        <!-- AI正在回答的loading状态 -->
        <div v-if="dataVal.isAIResponding" class="message received">
            <div class="img"></div>
            <div class="message-content text-l">
                <div class="ai-responding">
                    <van-loading size="24px" v-if="!dataVal.currentResponse">思考中......
                    </van-loading>
                    <template v-else>
                        <div class="partial-response" ref="messageDiv" v-html="dataVal.currentResponse"></div>
                    </template>
                </div>
            </div>
            <div class="message-time">
                {{
                    new Date().toLocaleTimeString([], {
                        hour: "2-digit",
                        minute: "2-digit",
                    })
                }}
            </div>
        </div>
    </scroll-view>
</template>
<style scoped lang="scss">
/* 聊天容器样式 */
.chat-container {
    width: 100%;
    height: 100%;
    margin-top: 10px;
    // padding-bottom: 20px;
    box-sizing: border-box;
    overflow-y: auto;

    .message {
        display: flex;
        flex-direction: column;
        max-width: 70%;
        padding: 12px;
        border-radius: 12px;
        margin-bottom: 20px;
        position: relative;
        word-wrap: break-word;
        font-size: 14px;
        line-height: 1.5;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        box-sizing: border-box;
        position: relative;

        // .iconCopy {
        //     width: 24px;
        //     height: 24px;
        //     position: absolute;
        //     right: 4px;
        //     bottom: 0;
        //     background: url("@/assets/images/icon-copy.png");
        //     background-size: cover;
        // }

        .volume {
            // width: 24px;
            // height: 24px;
            // position: absolute;
            //right: 4px;
            //bottom: 0;
        }

        .message-content {
            width: 100%;
        }

        .message-time {
            font-size: .75rem;
            color: #999;
            margin-top: 8px;
            text-align: right;
        }

        .voice-message {
            display: flex;
            align-items: center;
            gap: 8px;
            color: #07c160;
        }

        .ai-responding {
            .partial-response {
                margin-top: 10px;
                padding: 8px;
                background: #f5f5f5;
                border-radius: 8px;
            }
        }

        .img {
            position: absolute;
            width: 35px;
            height: 35px;
            border-radius: 50%;
            overflow: hidden;
            background-repeat: no-repeat;
            background-size: cover;
            border: 1px solid rgb(88 94 241);
        }
    }

    .sent {
        background-color: #E9529426;
        margin-left: auto;
        margin-right: 50px;
        border-top-right-radius: 4px;

        .img {
            position: absolute;
            top: 0;
            right: -45px;
            line-height: 60rpx;
        }
    }

    .received {
        background-color: #f8f8f8;
        margin-left: 50px;
        margin-right: auto;
        border-top-left-radius: 4px;

        .img {
            left: -45px;
            top: 0;
            background-image: v-bind("'url(' + dataVal.topAvatar + ')'");
        }
    }

    .open-form-link {
        color: #1989fa;
        text-decoration: underline;
        margin-top: 8px;
        cursor: pointer;
    }
}
</style>
